import os
import sys
from PIL import Image

def resize_image(input_path, output_path, width, height):
    """调整单张图像分辨率到指定的宽度和高度"""
    try:
        with Image.open(input_path) as img:
            resized_img = img.resize((width, height), Image.Resampling.LANCZOS)
            resized_img.save(output_path)
            print(f"成功将图像调整为 {width}x{height}，保存至 {output_path}")
    except Exception as e:
        print(f"错误: {e}，文件：{input_path}")
        sys.exit(1)

if __name__ == "__main__":
    input_dir = r'D:/AC_FUNCTION/innovate_pose/databases'
    output_dir = r'D:/AC_FUNCTION/innovate_pose/512-768'
    width = 512
    height = 768

    # 创建输出目录（如果不存在）
    os.makedirs(output_dir, exist_ok=True)

    # 遍历输入目录中的图像文件
    for filename in os.listdir(input_dir):
        file_path = os.path.join(input_dir, filename)
        if os.path.isfile(file_path) and filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
            output_path = os.path.join(output_dir, filename)
            resize_image(file_path, output_path, width, height)